|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Listing 7.2 ShareMemView.h and ShareMemView.cpp
// ShareMemView.h : interface of the CShareMemView class
//
/////////////////////////////////////////////////////////////////////////////
#if
!defined(AFX_SHAREMEMVIEW_H__BFF33AB7_A398_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_SHAREMEMVIEW_H__BFF33AB7_A398_11D1_887F_D42B07C10710__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CShareMemView : public CView
{
protected: // create from serialization only
CShareMemView();
DECLARE_DYNCREATE(CShareMemView)
// Attributes
public:
CShareMemDoc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CShareMemView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CShareMemView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CShareMemView)
afx_msg void OnFileWritememoryfile();
afx_msg void OnFileReadmemoryfile();
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in ShareMemView.cpp
inline CShareMemDoc* CShareMemView::GetDocument()
{ return (CShareMemDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.
#endif // !defined(AFX_SHAREMEMVIEW_H__BFF33AB7_A398_11D1_887F_D42B07C10710__INCLUDED_)
// ShareMemView.cpp : implementation of the CShareMemView class
//
#include stdafx.h
#include ShareMem.h
#include ShareMemDoc.h
#include ShareMemView.h
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CShareMemView
IMPLEMENT_DYNCREATE(CShareMemView, CView)
BEGIN_MESSAGE_MAP(CShareMemView, CView)
//{{AFX_MSG_MAP(CShareMemView)
ON_COMMAND(ID_FILE_WRITEMEMORYFILE, OnFileWritememoryfile)
ON_COMMAND(ID_FILE_READMEMORYFILE, OnFileReadmemoryfile)
ON_WM_CHAR()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShareMemView construction/destruction
CShareMemView::CShareMemView()
{ // TODO: add construction code here
}
CShareMemView::~CShareMemView()
{
}
BOOL CShareMemView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CShareMemView drawing
void CShareMemView::OnDraw(CDC* pDC)
{
CShareMemDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->TextOut(0, 0, pDoc->text);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CShareMemView printing
BOOL CShareMemView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CShareMemView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CShareMemView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CShareMemView diagnostics
#ifdef _DEBUG
void CShareMemView::AssertValid() const
{
CView::AssertValid();
}
void CShareMemView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CShareMemDoc* CShareMemView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShareMemDoc)));
return (CShareMemDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CShareMemView message handlers
void CShareMemView::OnFileWritememoryfile()
{
CShareMemDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
HANDLE MemoryMappingHandle;
MemoryMappingHandle = CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL,
PAGE_READWRITE, 0, 2048, MemoryFile);
LPVOID FilePointer = MapViewOfFile(MemoryMappingHandle, FILE_MAP_WRITE, 0, 0, 0);
::lstrcpy((char *) FilePointer, (LPCTSTR) pDoc->text);
UnmapViewOfFile((LPVOID) FilePointer);
if(FilePointer != NULL){
AfxMessageBox(Memory file written.);
}
}
void CShareMemView::OnFileReadmemoryfile()
{
CShareMemDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
HANDLE MemoryMappingHandle = NULL;
MemoryMappingHandle = OpenFileMapping(FILE_MAP_READ, FALSE,
MemoryFile);
LPVOID FilePointer = MapViewOfFile(MemoryMappingHandle, FILE_MAP_READ |
FILE_MAP_WRITE, 0, 0, 0);
pDoc->text = (char *) FilePointer;
UnmapViewOfFile((LPVOID) FilePointer);
CloseHandle(MemoryMappingHandle);
Invalidate();
}
void CShareMemView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CShareMemDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->text += nChar;
Invalidate();
CView::OnChar(nChar, nRepCnt, nFlags);
}
Whats AheadIn the next chapter, we continue with our guided tour of Visual C++ and start working with a very popular topic: connecting to the Internet. We learn to set up a Web browser in Visual C++ and how to use the HTTP and FTP protocols.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |